package com.atgugu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/4/11 9:12
 */
public class Flink09_SQL_Kafka {
    public static void main(String[] args) {
        // 流->动态表->连续查询->动态表->流
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
       
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 执行ddl语句, 建表 A, 与文件直接关联
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka'," +
                            "  'topic' = 's1'," +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092'," +
                            "  'properties.group.id' = 'atguigu'," +
                            "  'scan.startup.mode' = 'latest-offset'," +
                            "  'format' = 'json' " +
                            ")");
    
        Table table = tEnv.sqlQuery("select * from sensor where id='sensor_1'");
    
    
        tEnv.executeSql("create table a(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka'," +
                            "  'topic' = 's2'," +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092'," +
                            "  'format' = 'json', " +
                            "  'sink.partitioner' = 'round-robin' " +
                            ")");
    
        table.executeInsert("a");
    
    }
}
